/***************************************************************************
 *   Copyright (C) 2008 by Philipp Müller                                  *
 *   philipp.mueller@gmx.de                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
 ***************************************************************************/

#include <QHeaderView>

#include "mediawidgetitem.h"
#include "vompglobal.h"

bool MediaWidgetItem::operator < ( const QTreeWidgetItem & other ) const
{
    const MediaWidgetItem * myItem = this;
    const MediaWidgetItem * otherItem = ( MediaWidgetItem * ) & other;

    bool reverseOrder = ( treeWidget()->header()->sortIndicatorOrder() == Qt::DescendingOrder );

    if ( myItem->entryType() == MEDIA_TYPE_DIR ) // directories still sorted by text
    {
        if ( otherItem->entryType() == MEDIA_TYPE_DIR ) //directories are sorted by Text, otherwise dirs are on top
        {
            QString myText = text( 0 ).toLower();
            QString otherText = other.text( 0 ).toLower();
            return ( myText > otherText ) && reverseOrder;
        }
        else
        {
            return false && reverseOrder; //Dirs always come first ascending
        }
    }
    else
    {
        if ( otherItem->entryType() == MEDIA_TYPE_DIR ) // directories still sorted by text
        {
            return true && reverseOrder; //Dirs always come first ascending
        }
        else
        {
            if ( treeWidget()->sortColumn() == 1 ) //sort by date for the files
            {
                return myItem->dateTime() < otherItem->dateTime();
            }
            else
            {
                QString myText = text( 0 ).toLower();
                QString otherText = other.text( 0 ).toLower();
                return myText > otherText;
            }
        }
    }
}


QString MediaWidgetItem::path() const
{
    if ( parent() == 0L )
    {
        return m_rootPath;
    }
    else
    {
        QString path;
        path.append( static_cast<MediaWidgetItem*>( parent() )->path() );
        path.append( '/' );
        path.append( text( 0 ) );
        return path;
    }
}


void MediaWidgetItem::setPath( const QString& path )
{
    if ( parent() == 0L )
    {
        m_rootPath = path;
        return;
    }
}


QDateTime MediaWidgetItem::dateTime() const
{
    return m_dateTime;
}


void MediaWidgetItem::setDateTime( const QDateTime& dateTime )
{
    m_dateTime = dateTime;
}


int MediaWidgetItem::entryType() const
{
    return m_type;
}


void MediaWidgetItem::setEntryType( int type )
{
    m_type = type;
}
